package com.unieagle.exam.http;

import java.io.File;
import java.util.Collection;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.android.volley.Request;
import com.unieagle.exam.account.AccountManager;
import com.unieagle.exam.app.ExamApplication;
import com.unieagle.exam.model.CheckVersionResult;
import com.unieagle.exam.model.UserInfoResult;
import com.unieagle.exam.model.VerificationCodeResult;
import com.unieagle.exam.utils.Base64Utils;

public class TaskHelper {

    public static VolleyTask createRegisterTask(String name, String certificate,String phone, String pushToken){
        VolleyTask task = new VolleyTask();
        String url = BusinessConfig.getRegisterUri();
        task.setUrl(url);
        String content = BusinessConfig.getRegisterContent(name, certificate, phone, pushToken);
        task.setContent(content);
        task.setMethod(Request.Method.POST);
        task.setResponseType(UserInfoResult.class);
        return task;
    }

    public static VolleyTask createVerificationCodeTask(String phone){
        VolleyTask task = new VolleyTask();
        String url = BusinessConfig.getVerificationCodeUri();
        task.setUrl(url);
        String content = BusinessConfig.getVerificationCodeContent(phone);
        task.setContent(content);
        task.setMethod(Request.Method.POST);
        task.setResponseType(VerificationCodeResult.class);
        return task;
    }

    public static VolleyTask createSigninTask(String phone, String verificationCode, String pushToken){
        VolleyTask task = new VolleyTask();
        String url = BusinessConfig.getSigninUri();
        task.setUrl(url);
        String content = BusinessConfig.getSigninContent(phone, verificationCode, pushToken);
        task.setContent(content);
        task.setMethod(Request.Method.POST);
        task.setResponseType(UserInfoResult.class);
        return task;
    }

    public static VolleyTask createUploadImageTask(String imageType, File imagePath){
        VolleyTask task = new VolleyTask();
        String url = BusinessConfig.getUploadImageUri(String.valueOf(AccountManager.getInstance().getAccountId()));
        task.setUrl(url);
        String content = BusinessConfig.getUploadImageContent(AccountManager.getInstance().getAccessToken(), imageType, "data:image/jpeg;base64," + Base64Utils.encode(imagePath));
        task.setContent(content);
        task.setMethod(Request.Method.POST);
        task.setResponseType(UserInfoResult.class);
        task.setPolocy(VolleyRequest.getInstance().createPolicy(15000,3));
        return task;
    }

    public static VolleyTask createRequestActivationTask(){
        VolleyTask task = new VolleyTask();
        String url = BusinessConfig.getRequestActivationUri(String.valueOf(AccountManager.getInstance().getAccountId()));
        task.setUrl(url);
        String content = BusinessConfig.getRequestActivationContent(AccountManager.getInstance().getAccessToken());
        task.setContent(content);
        task.setMethod(Request.Method.POST);
        task.setResponseType(UserInfoResult.class);
        return task;
    }

    public static VolleyTask createSelectSubjectsTask(Collection<Integer> subjects, Boolean remove){
        VolleyTask task = new VolleyTask();
        String url = BusinessConfig.getSelectSubjectsUri(String.valueOf(AccountManager.getInstance().getAccountId()));
        task.setUrl(url);
        String content = BusinessConfig.getSelectSubjectsContent(AccountManager.getInstance().getAccessToken(), subjects, remove);
        task.setContent(content);
        task.setMethod(Request.Method.POST);
        task.setResponseType(UserInfoResult.class);
        return task;
    }

    public static VolleyTask createCheckVersionTask(){
        VolleyTask task = new VolleyTask();
        Context context = ExamApplication.instance.getApplicationContext();
        PackageManager packageManager=context.getPackageManager();
        PackageInfo packageInfo;
        String versionName="";
        try {
            packageInfo=packageManager.getPackageInfo(context.getPackageName(),0);
            versionName=packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String url = BusinessConfig.getCheckVersionUri("android", versionName);
        task.setUrl(url);
        task.setMethod(Request.Method.GET);
        task.setRequestClass(VolleyRequest.FormatGsonRequest.class);
        task.setResponseType(CheckVersionResult.class);
        task.setPolocy(VolleyRequest.getInstance().createPolicy(10000,3));
        return task;
    }

    public static VolleyTask createCheckVersionTask2(){
        VolleyTask task = new VolleyTask();
        Context context = ExamApplication.instance.getApplicationContext();
        PackageManager packageManager=context.getPackageManager();
        PackageInfo packageInfo;
        String versionName="";
        try {
            packageInfo=packageManager.getPackageInfo(context.getPackageName(),0);
            versionName=packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String url = BusinessConfig.getCheckVersionUri();
        task.setUrl(url);
        String content = BusinessConfig.getCheckVersionContent("android", versionName);
        task.setContent(content);
        task.setMethod(Request.Method.POST);
        task.setResponseType(CheckVersionResult.class);
        task.setPolocy(VolleyRequest.getInstance().createPolicy(10000,3));
        return task;
    }
    
}
